import axios from "axios";
import { ElMessage } from "element-plus";

axios.defaults.headers["Content-Type"] = "application/json; charset=utf-8";

const service = axios.create({
  // baseURL: import.meta.env.VITE_APP_BASE_API,
  baseURL: '/api',
  timeout: 20000,
});

service.interceptors.request.use(
  (config) => {
    const isToken = config.headers.token;
    if (!isToken) {
      config.headers.token = localStorage.getItem("token") || ""
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  (response) => {
    const result = response.data;

    if (response.status !== 200) {
      ElMessage.error(result.message);
    } else {
        // console.log("response",response)

      return result;
    }
  },
  (error) => {
      console.log(error)
    return Promise.reject(error);
  }
);

export default service;
